<template>
  <div class="notice-management">
    <el-card>
      <!-- 搜索和操作按钮 -->
      <template #header>
        <div class="card-header">
          <div class="search-form">
            <el-input
              v-model="searchTitle"
              placeholder="搜索标题"
              style="width: 200px"
              clearable
              @clear="handleSearch"
            />
            <el-select 
              v-model="searchStatus" 
              placeholder="状态"
              clearable
              style="width: 120px"
            >
              <el-option label="未发布" :value="0" />
              <el-option label="已发布" :value="1" />
            </el-select>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
          <el-button type="success" @click="handleAdd">新增公告</el-button>
        </div>
      </template>

      <!-- 公告列表 -->
      <el-table :data="notices" v-loading="loading" style="width: 100%">
        <el-table-column prop="title" label="标题" show-overflow-tooltip />
        <el-table-column prop="content" label="内容" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '已发布' : '未发布' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDateTime(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="scope.row.status === 0"
              type="success"
              size="small"
              @click="handlePublish(scope.row)"
            >
              发布
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="form.id ? '编辑公告' : '新增公告'"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :rows="4"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { formatDateTime } from '@/utils/dateUtil'

const loading = ref(false)
const dialogVisible = ref(false)
const formRef = ref()
const notices = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const searchTitle = ref('')
const searchStatus = ref('')

const form = reactive({
  id: null,
  title: '',
  content: '',
  status: 0
})

const rules = {
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
}

// 加载公告列表
const loadNotices = async () => {
  try {
    loading.value = true
    const res = await request.get('/admin/notices', {
      params: {
        current: currentPage.value,
        size: pageSize.value,
        title: searchTitle.value,
        status: searchStatus.value
      }
    })
    if (res.code === 200) {
      // 添加调试日志
      console.log('Notices data:', res.data.records)
      if (res.data.records.length > 0) {
        console.log('First notice createdAt:', res.data.records[0].createdAt)
        console.log('createdAt type:', typeof res.data.records[0].createdAt)
        if (Array.isArray(res.data.records[0].createdAt)) {
          console.log('createdAt array content:', res.data.records[0].createdAt)
        }
      }
      notices.value = res.data.records
      total.value = res.data.total
    }
  } catch (error) {
    console.error('Failed to load notices:', error)
    ElMessage.error('加载公告列表失败')
  } finally {
    loading.value = false
  }
}

// 其他方法...

loadNotices()
</script>

<style scoped>
.notice-management {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  display: flex;
  gap: 10px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 